using Assets.Scripts.GDK;
using UnityEngine;

namespace Brick
{
	public class MuzzuleBox : gdk_base_UI
	{
		public LineManager lineMgr;

		private Vector2 shootPosition;

		private Vector2 muzzlePos;

		public Transform tempGO;

		private bool onceShootOver;

		private bool curCanShoot;

		public override void OnLoad()
		{
			gdk_game_manager.AddEventListener(EventID.GAME_SHOOP_START, SetCanShoot);
			gdk_game_manager.AddEventListener<Vector2>(EventID.GAME_SHOOT_MOVE, showSelectMoveLine);
			gdk_game_manager.AddEventListener(EventID.GAME_SHOOT_CANCLE, hideMoveLine);
			gdk_game_manager.AddEventListener(EventID.GAME_SHOOT_OUT, shootingOut);
			gdk_game_manager.AddEventListener<int>(EventID.GAME_PLAY_START, showEnemy);
		}

		public override void UnLoad()
		{
			gdk_game_manager.RemoveEventListener(EventID.GAME_SHOOP_START, SetCanShoot);
			gdk_game_manager.RemoveEventListener<Vector2>(EventID.GAME_SHOOT_MOVE, showSelectMoveLine);
			gdk_game_manager.RemoveEventListener(EventID.GAME_SHOOT_CANCLE, hideMoveLine);
			gdk_game_manager.RemoveEventListener(EventID.GAME_SHOOT_OUT, shootingOut);
			gdk_game_manager.RemoveEventListener<int>(EventID.GAME_PLAY_START, showEnemy);
		}

		public override void OnStart()
		{
			lineMgr.HideAll();
		}

		public void onShowEvent()
		{
			base.gameObject.SetActive(value: true);
		}

		public void onHideEvent()
		{
			base.gameObject.SetActive(value: false);
		}

		public void SetCanShoot()
		{
			muzzlePos = getMuzzle();
			GDK.ModuleManger.Module<BallFindWayModule>().SetShoot(haveMuzzel: false);
			GDK.ModuleManger.Module<MuzzuleBoxModule>().SetCanShoot(bShoot: true);
			onceShootOver = false;
			lineMgr.changeBigBallBg();
		}

		public void showEnemy(int level)
		{
			muzzlePos = getMuzzle();
			lineMgr.OnCreateBalls(muzzlePos);
		}

		private void showSelectMoveLine(Vector2 movePos)
		{
			if (onceShootOver)
			{
				return;
			}
			tempGO.position = movePos;
			Vector2 b = tempGO.localPosition;
			muzzlePos = getMuzzle();
			float num = Vector2.Angle(Vector2.up, (muzzlePos - b).normalized);
			if (num <= 97f)
			{
				if (num <= 90f)
				{
					curCanShoot = false;
					hideMoveLine();
				}
			}
			else
			{
				shootPosition = b;
				curCanShoot = true;
				Vector2 vector = shootPosition - muzzlePos;
				lineMgr.Change(vector.normalized, muzzlePos);
			}
		}

		private void hideMoveLine()
		{
			lineMgr.HideAll();
		}

		private void shootingOut()
		{
			if (curCanShoot)
			{
				OnShooting();
			}
		}

		private void OnShooting()
		{
			if (!onceShootOver)
			{
				onceShootOver = true;
				GDK.ModuleManger.Module<MuzzuleBoxModule>().SetCanShoot(bShoot: false);
				Vector2 obj = shootPosition - muzzlePos;
				gdk_game_manager.DispachEvent(EventID.GAME_BALL_FORCE, obj);
				gdk_game_manager.DispachEvent(EventID.SHOW_PLAY_RECORD);
				lineMgr.HideAll();
			}
		}

		private Vector3 getMuzzle()
		{
			return GDK.ModuleManger.Module<BallFindWayModule>().getShootPos();
		}
	}
}

